package com.basic.emg.mapper;


import com.basic.emg.domain.*;
import io.lettuce.core.dynamic.annotation.Param;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

/**
 * 应急预案
 * @Author: Chn
 * @Create: 2024/10/22
 * @Version: 1.0
 */
public interface EmergencyPlanMapper extends Mapper<EmergencyPlan> {

    @Insert("insert into emergency_plan (plan_name,plan_type,plan_version,create_time,update_time,pub_time,plan_status,del_flag,file_path,remark)\n" +
            "values ( #{planName},#{planType},#{planVersion},localtimestamp,localtimestamp,#{pubTime},#{planStatus},'0',#{filePath},#{remark})")
    @SelectKey(statement = "SELECT currval('seq_emergency_plan_id')", keyProperty = "id", before = false, resultType = long.class)
    public int insertPlan( EmergencyPlan plan);

    @Select("select et.* from plan_team pt,emergency_team et,emergency_plan ep where pt.team_id = et.id and ep.id = pt.plan_id and pt.plan_id = #{planId}")
    public List<EmergencyTeam> selectPlanTeam(@Param("planId")Long planId);

    @Select("select et.* from plan_mat pt,emergency_material et,emergency_plan ep where pt.mat_id = et.id and ep.id = pt.plan_id and pt.plan_id = #{planId}")
    public List<EmergencyMaterial> selectPlanMat(@Param("planId")Long planId);

    @Insert("<script> INSERT INTO plan_team (plan_id, team_id) VALUES " +
            "    <foreach collection='teamList' item='item' index='index' separator=','>\n" +
            "        (#{item.planId}, #{item.teamId})\n" +
            "    </foreach>" +
            "</script>")
    public int insertPlanTeam(@Param("teamList") List<PlanTeam> teamList);

    @Insert("<script> INSERT INTO plan_mat (plan_id, mat_id) VALUES " +
            "    <foreach collection='matList' item='item' index='index' separator=','>\n" +
            "        (#{item.planId}, #{item.matId})\n" +
            "    </foreach>" +
            "</script>")
    public int insertPlanMat(@Param("matList") List<PlanMat> matList);


    @Delete("delete from plan_team where plan_id = #{planId}")
    public int deletePlanTeam(@Param("planId") Long planId);

    @Delete("delete from plan_mat where plan_id = #{planId}")
    public int deletePlanMat(@Param("planId") Long planId);

}
